﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class OnlineBookingDetailForm : DevExpress.XtraEditors.XtraForm
    {
        private const string m_ModuleId = "0201";

        private string m_BookingId;
        private OnlineBookingModel onlineBookingModel;

        public OnlineBookingDetailForm(string bookingId)
        {
            InitializeComponent();

            this.m_BookingId = bookingId;
            this.txtRemark.Properties.MaxLength = 1024;
          
        }


        private void btnSaveCustomer_Click(object sender, EventArgs e)
        {
            try
            {

                Global.Instance.BeginWait();

            
                string id = this.m_BookingId;
                string name = this.txtCustomerName.Text.Trim();
                string mobileNumber = this.txtCustomerMobileNumber.Text.Trim();


                if (!this.ModifyBooking())
                {
                    Global.Instance.ErrorDlg("保存失败！");
                    return;
                }
                //StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, string.Format("修改编码为“{0} - {1} - ”的记录", id, name, mobileNumber));

            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnCancelCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void AppendCustomerForm_Load(object sender, EventArgs e)
        {
            this.onlineBookingModel = OnlineBookingDAL.Instance.Get(new QueryOnlineBookingModel() { Id = this.m_BookingId });
            this.txtCustomerName.Text = this.onlineBookingModel.CustomerName;
            this.txtCustomerMobileNumber.Text = this.onlineBookingModel.CustomerPhone;
            this.txtCustomerWeChat.Text = this.onlineBookingModel.WeChat;
            this.txtCustomerEmail.Text = this.onlineBookingModel.EMail;
            this.txtQq.Text = this.onlineBookingModel.QqNumber;
            this.txtDepCity.Text = this.onlineBookingModel.DepartureCityName;
            this.txtArrvalCity.Text = this.onlineBookingModel.ArrivalCityName;
            if (this.onlineBookingModel.DepartureDate.HasValue)
            {
                this.dtDepDate.DateTime = this.onlineBookingModel.DepartureDate.Value;
            }
            if (this.onlineBookingModel.ReturnDate.HasValue)
            {
                this.dtArrvialDate.DateTime = this.onlineBookingModel.ReturnDate.Value;
            }
            this.cmbHandle.SelectedIndex = this.onlineBookingModel.Handle;
            this.cmbReturn.SelectedIndex = this.onlineBookingModel.TravelType;
            this.txtRemark.Text = this.onlineBookingModel.Remark;
        }


        private bool ModifyBooking()
        {
            try
            {
                OnlineBookingModel model = new OnlineBookingModel()
                {
                    Id = this.m_BookingId,
                    CustomerName = this.txtCustomerName.Text.Trim(),
                    CustomerPhone = this.txtCustomerMobileNumber.Text,
                    DepartureCityName = this.txtDepCity.Text.Trim(),
                    ArrivalCityName = this.txtArrvalCity.Text.Trim(),
                    EMail = this.txtCustomerEmail.Text.Trim(),
                    QqNumber = this.txtQq.Text.Trim(),
                    WeChat = this.txtCustomerWeChat.Text.Trim(),
                    Handle = this.cmbHandle.SelectedIndex,
                    TravelType = this.cmbReturn.SelectedIndex,
                    Remark=this.txtRemark.Text
                };

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            OnlineBookingDAL.Instance.Update(tran, model);

                            tran.Commit();

                            this.onlineBookingModel = model;
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }
    }
}